#include "referee_usart_task.h"

uint8_t usart1_RX_buf[USART_RX_BUF_LENGTH];

/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
	
  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
	if(RESET != __HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE))   //锟叫讹拷锟角凤拷锟角匡拷锟斤拷锟叫讹拷
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart1);                       //锟斤拷锟斤拷锟斤拷锟斤拷卸媳锟街?
		HAL_UART_DMAStop(&huart1);      
		extern uint8_t usart1_RX_buf[128];		//停止锟斤拷锟斤拷DMA锟斤拷锟斤拷     
    uint16_t CMD_ID = 0;
		uint16_t buffer_index = 0; 
    uint16_t buffer_index_max =  127;
    while(buffer_index<buffer_index_max)
    {
  			
			if ((usart1_RX_buf[buffer_index]==0xA5) && 
					(verify_CRC8_check_sum(&usart1_RX_buf[buffer_index],5)==1))
			{
					//锟斤拷锟捷达拷锟斤拷锟斤拷锟斤拷
				uint16_t cmd_id=(usart1_RX_buf[buffer_index+6])&0xff;
				cmd_id=(cmd_id<<8)|usart1_RX_buf[buffer_index+5];  
				uint16_t data_length=usart1_RX_buf[buffer_index+2]&0xff;
				data_length=(data_length<<8)|usart1_RX_buf[buffer_index+1];
				CMD_ID = cmd_id;
				switch (CMD_ID)
				{
						case 0x0105://0x0105 锟斤拷锟节凤拷锟斤拷诘锟斤拷锟绞?
						{
								if((buffer_index_max-buffer_index>=(sizeof(ext_dart_remaining_time_t)+7))&&
								verify_CRC16_check_sum(&usart1_RX_buf[buffer_index],sizeof(ext_dart_remaining_time_t)+7+2)==1
								)
								{
										memcpy(&dart_remaining_time_t, &usart1_RX_buf[buffer_index+7], sizeof(ext_dart_remaining_time_t));
										buffer_index+=sizeof(ext_dart_remaining_time_t)+7;
								}
						}
						case 0x020A://0x0105 锟斤拷锟节凤拷锟斤拷诘锟斤拷锟绞?
						{
								if((buffer_index_max-buffer_index>=(sizeof(ext_dart_client_cmd_t)+7))&&
								verify_CRC16_check_sum(&usart1_RX_buf[buffer_index],sizeof(ext_dart_client_cmd_t)+9)==1
								)
								{
										memcpy(&dart_client_cmd_t, &usart1_RX_buf[buffer_index+7], sizeof(ext_dart_client_cmd_t));
										buffer_index+=sizeof(ext_dart_client_cmd_t)+7;
								}
						}
				}
			}
      buffer_index++;
    } 
  }		
		HAL_UART_Receive_DMA(&huart1, (uint8_t*)usart1_RX_buf, 128);                    //锟斤拷锟斤拷锟斤拷始DMA锟斤拷锟斤拷
  /* USER CODE END USART1_IRQn 1 */
}



